package com.xkdx.guangguang.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.shareclass.ProductInfo;

public class UnitInfoAdapter extends BaseAdapter {
	List<ProductInfo> list;
	Context context;
	ImageLoader imageLoader;
	DisplayImageOptions options = new DisplayImageOptions.Builder()
			.showStubImage(R.drawable.shop_empty)
			.showImageForEmptyUri(R.drawable.shop_empty)
			.showImageOnFail(R.drawable.shop_empty).resetViewBeforeLoading()
			.cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY)
			.bitmapConfig(Bitmap.Config.RGB_565).build();

	public UnitInfoAdapter(Context context, List<ProductInfo> list,
			ImageLoader imageLoader) {
		this.list = list;
		this.context = context;
		this.imageLoader = imageLoader;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHodler viewHoder;

		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.shop_unit_item, null);
			viewHoder = new ViewHodler();
			viewHoder.logo = (ImageView) convertView
					.findViewById(R.id.shop_unititem_logo);
			viewHoder.name = (TextView) convertView
					.findViewById(R.id.shop_unititem_name);
			viewHoder.price = (TextView) convertView
					.findViewById(R.id.shop_unititem_price);
			viewHoder.num = (TextView) convertView
					.findViewById(R.id.shop_unititem_exchangenum);
			convertView.setTag(viewHoder);
		} else {
			viewHoder = (ViewHodler) convertView.getTag();
		}

		imageLoader.displayImage(list.get(position).getLogo(), viewHoder.logo,
				options);
		viewHoder.name.setText(list.get(position).getProductName());
		String price = String.valueOf(Float.parseFloat(list.get(position).getProductPrice()) / 100);
		viewHoder.price.setText("¥" + price+"元");
		viewHoder.num
				.setText("已售" + list.get(position).getExChange_Num() + "笔");

		return convertView;
	}

	class ViewHodler {
		TextView name, price, num;
		ImageView logo;

	}
}